package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.domain.Building;
import com.ruoyi.system.domain.vo.ResidentVo;
import com.ruoyi.system.service.IBuildingService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Resident;
import com.ruoyi.system.service.IResidentService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 小区居民信息Controller
 * 
 * @author 祖新宇
 * @date 2024-12-17
 */
@Controller
@RequestMapping("/system/resident")
public class ResidentController extends BaseController
{
    private String prefix = "system/resident";

    @Autowired
    private IResidentService residentService;
    @Resource
    private IBuildingService buildingService;

    @RequiresPermissions("system:resident:view")
    @GetMapping()
    public String resident()
    {
        return prefix + "/resident";
    }

    /**
     * 查询小区居民信息列表
     */
    @RequiresPermissions("system:resident:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Resident resident)
    {
        startPage();
        resident.setIsDelete(0);
        List<Resident> list = residentService.selectResidentList(resident);
        List<ResidentVo> residentVos = new ArrayList<>();
        list.forEach(r -> {
            Long buildingId = r.getBuildingId();
            Building building = buildingService.selectBuildingById(buildingId);
            ResidentVo residentVo = new ResidentVo();
            BeanUtils.copyProperties(r,residentVo);
            residentVo.setBuildingName(Optional.ofNullable(building).map(Building::getBuildingName).orElse("--"));
            residentVos.add(residentVo);
        });
        return getDataTable(residentVos);
    }

    /**
     * 导出小区居民信息列表
     */
    @RequiresPermissions("system:resident:export")
    @Log(title = "小区居民信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Resident resident)
    {
        List<Resident> list = residentService.selectResidentList(resident);
        ExcelUtil<Resident> util = new ExcelUtil<Resident>(Resident.class);
        return util.exportExcel(list, "小区居民信息数据");
    }

    /**
     * 新增小区居民信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<Building> buildings = buildingService.selectBuildingList(new Building());
        mmap.put("buildings",buildings);
        return prefix + "/add";
    }

    /**
     * 新增保存小区居民信息
     */
    @RequiresPermissions("system:resident:add")
    @Log(title = "小区居民信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Resident resident)
    {
        resident.setCreateTime(new Date());
        resident.setUpdateTime(new Date());
        Long userId = ShiroUtils.getUserId();
        resident.setCreateUserId(String.valueOf(userId));
        resident.setUpdateUserId(String.valueOf(userId));
        return toAjax(residentService.insertResident(resident));
    }

    /**
     * 修改小区居民信息
     */
    @RequiresPermissions("system:resident:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Resident resident = residentService.selectResidentById(id);
        mmap.put("resident", resident);
        List<Building> buildings = buildingService.selectBuildingList(new Building());
        mmap.put("buildings",buildings);
        return prefix + "/edit";
    }

    /**
     * 修改保存小区居民信息
     */
    @RequiresPermissions("system:resident:edit")
    @Log(title = "小区居民信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Resident resident)
    {
        resident.setUpdateTime(new Date());
        Long userId = ShiroUtils.getUserId();
        resident.setUpdateUserId(String.valueOf(userId));
        return toAjax(residentService.updateResident(resident));
    }

    /**
     * 删除小区居民信息
     */
    @RequiresPermissions("system:resident:remove")
    @Log(title = "小区居民信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(residentService.softDeleteResidentByIds(ids));
    }
}
